import {getResumeList, setResumeAdd, setResumeEdit} from "@/api/api";
import {initYMD} from "@/sever/utils";

export const commonObj = {
    data() {
        return {
            options: {
                disabledDate(date) {
                    return date && date.valueOf() > Date.now();
                }
            },
            fseId:"",//特种设备id
            resumePageNum:1,//履历分页下标
            resumePageSize:8,//履历分页条数
            resumeCurrent:1,//履历当前分页下标
            resumeTotal:0,//履历总数
            commonFlag:false,//编辑节流阀
            tabIndex:0,//自定义切换栏下标
            commonTitle:'',//公共弹框标题
            wordList:['doc','docx'],//word-格式校验
            excelList:['xls','xlsx'],//excel-格式校验
            pptList:['pptx','ppt'],//ppt-格式校验
            pdfList:['pdf'],//pdf'-格式校验
            imgList:['BMP','JPG','JPEG','PNG','GIF','gif','png','jpeg','jpg','bmp'],//img-格式校验
            txtList:['txt'],//txt-格式校验
            videoList:['AVI','MOV','QT','ASF','RM','NAVI'],//video-格式校验
            zipList:['RAR','ZIP','ARJ','Z','zip','rar','arj','z'],//zip-格式校验
            audioList:['acm','aif','AIF','AIFC','AIFF','asf','asp','asx','mp3','wma','avi','rm','rmvb','flv','mpg','mov','mkv'],//audio-格式校验
            //文件类型限制
            acceptObj:".doc,.docx,.xls,.xlsx,.pptx,.pdf,.BMP,.JPG,.JPEG,.PNG,.GIF,.txt,.AVI,.MOV,.QT,.ASF,.RM,.NAVI,.acm,.aif,.AIF,.AIFC,.AIFF,.asf,.asp,.asx,.mp3,.wma,.avi,.rm,.rmvb,.flv,.mpg,.mov,.mkv,.RAR,.ZIP,.ARJ,.Z,",
            checkUserList:[],//检查人列表
            device_install:{//设备安装对象
                name:'',//安装名称
                part:'',//安装部位
                startTime:'',//安装时间
                attachUrl:[],//安装图片
            },
            submit_device_install:{//提交设备安装对象
                name:'',//提交安装名称
                part:'',//提交安装部位
                startTime:new Date(),//提交安装时间
                attachUrl:[],//提交安装图片
            },
            fileType:'',//文件类型后缀
            tableList:[],//表格列表数据
            stateList: [
                {
                    id: 1,
                    name: "正常"
                },
                {
                    id: 0,
                    name: "异常"
                }
            ],//状态列表数据
            btnList:[],//权限code
            editData:{},//编辑数据对象
            editFlag:false,//是否编辑
            delModal:false,//履历删除弹框
            delContent:'',//履历删除内容
            delId:'',//履历删除id
        }
    },

    watch:{
        detailActiveName(val, oldVal){
            this.tableList = [];
            this.resumePageNum = 1;
            this.resumeCurrent = 1;
            if(val == 4){
                this.tabIndex = 1;
                this.getResumeListFn();
            }else if(val == 5){
                this.commonTitle = '添加设备检查信息';
                this.delContent = '您是否确认删除设备检查记录？';
                this.tabIndex = 2;
                this.getResumeListFn();
            }else if(val == 6){
                this.commonTitle = '添加设备使用信息';
                this.delContent = '您是否确认删除设备使用记录？';
                this.tabIndex = 3;
                this.getResumeListFn();
            }else if(val == 7){
                this.commonTitle = '添加设备维护信息';
                this.delContent = '您是否确认删除设备维护记录？';
                this.tabIndex = 4;
                this.getResumeListFn();
            }else if(val == 8){
                this.commonTitle = '添加设备拆卸信息';
                this.delContent = '您是否确认删除设备拆卸记录？';
                this.tabIndex = 5;
                this.getResumeListFn();
            }
        },
    },
    methods: {
        //编辑
        setEdit(item,active){
            if(active == 2){
                this.commonTitle = '编辑设备检查信息';
            }else if(active == 3){
                this.commonTitle = '编辑设备使用信息';
            }else if(active == 4){
                this.commonTitle = '编辑设备维护信息';
            }else if(active == 5){
                this.commonTitle = '编辑设备拆卸信息';
            }
            let obj = {};
            this.tabIndex = active;
            this.resumeId = item.resumeId;
            for (const itemKey in item) {
                obj[itemKey] = item[itemKey];
            }
            this.editData = obj;
            this.commonFlag = true;
            this.editFlag = true;
        },
        //删除
        removeRow(resumeId){
            this.delModal = true;
            this.delId = String(resumeId);
        },
        handleDelSubmit(delModal){
            this.delModal = delModal;
            this.resumePageNum = 1;
            this.resumeCurrent = 1;
            this.getResumeListFn();
        },
        handleDelClose(delModal){
            this.delModal = delModal;
        },
        resumePageChange(index){
            this.resumePageNum = index;
            this.getResumeListFn();
        },
        //添加特种设备履历
        setResumeAddFn(params){
            params = JSON.parse(params);
            if(params.startTime){
                params.startTime = initYMD(String(params.startTime)) +' ' + '00:00:00'
            }
            if(params.endTime){
                params.endTime = initYMD(params.endTime) +' ' + '00:00:00'
            }
            params.fseId = this.fseId;
            params.fseType = this.fseType;
            params.resumeType = this.tabIndex;
            if(this.editFlag){
                params.resumeId = this.resumeId;
                setResumeEdit(params).then(res => {
                    if(res.code == 200){
                        this.getResumeListFn();
                    }else{
                        this.$Message.warning(res.msg);
                    }
                })
                    .catch(error => console.log(error))
            }else{
                setResumeAdd(params).then(res => {
                    if(res.code == 200){
                        this.getResumeListFn();
                    }else{
                        this.$Message.warning(res.msg);
                    }
                })
                    .catch(error => console.log(error))
            }

        },
        //获取特种设备履历
        getResumeListFn(){
            let params = {
                fseId :this.fseId,
                resumeType :this.tabIndex,
                pageNum:this.resumePageNum,
                pageSize:this.resumePageSize,
            }
            getResumeList(params).then(res => {
                if(res.code == 200){
                    let list = res.data.list;
                    list.forEach(v =>{
                        if(v.startTime){
                            v.startTime = initYMD(v.startTime)
                        }
                        if(v.endTime){
                            v.endTime = initYMD(v.endTime)
                        }
                    })
                    if(this.tabIndex == 1){
                        if(list.length > 0){
                            let obj = list[0];
                            for (const key in obj) {
                                this.device_install[key] =  obj[key];
                                this.submit_device_install[key] =  obj[key];
                            }
                        }else{
                            this.device_install = {//设备安装对象
                                name:'',//安装名称
                                part:'',//安装部位
                                startTime:'',//安装时间
                                attachUrl:[],//安装图片
                            }
                            this.submit_device_install = {//提交设备安装对象
                                name:'',//提交安装名称
                                part:'',//提交安装部位
                                startTime:new Date(),//提交安装时间
                                attachUrl:[],//提交安装图片
                            }
                        }
                    }else{
                        this.tableList = list;
                        this.resumeTotal = res.data.total;
                    }
                }else{
                    this.$Message.warning(res.msg)
                }
            })
            .catch(error => console.log(error))
        },
        //接受子组件传递过来的数据
        handleClose(e){
            this.commonFlag = e;
        },
        //接受子组件传递过来的数据
        handleSubmit(active,params,flag){
            this.commonFlag = flag;
            this.save(active,JSON.stringify(params))
        },
        //tab切换
        setTab(){
            this.fvsFlag = false;
        },
        //设置节流阀
        setCommonFlag(active){
            this.commonFlag = true;
            this.tabIndex = active;
            this.editData = {};
            this.editFlag = false;
        },
        //数据提交
        save(active,params){
            if(active == 1){
                this.fvsFlag = false;
                this.setResumeAddFn(JSON.stringify(this.submit_device_install))
            }else{
                this.setResumeAddFn(params)
            }
        },
        //删除文件
        delFile(index){
            this.submit_device_install.attachUrl.splice(index,1)
        },
        //图片上传成功
        uploadSuccess(res, file) {
            let path = [...this.submit_device_install.attachUrl];
            path.push( res.data.path)
            this.submit_device_install.attachUrl = path
            this.$Message.success("上传成功");
        },
        // 图片大小检查
        beforeAvatarUpload(file) {
            this.fileType = this.extname(file.name);
            const isLt2M = file.size / 1024 / 1024 < 20;
            if (!isLt2M) {
                this.$Message.error("上传附件不能超过 20MB!");
            }
            return;
        },
        // 下载附件
        download(url){
            if(url){
                window.open(url);
            }else{
                this.$Message.warning("暂无附件");
            }
        },
        extname(filename) {
            if (!filename || typeof filename != "string") {
                return false;
            }
            let a = filename.split("").reverse().join("");
            let b = a.substring(0, a.search(/\./)).split("").reverse().join("");
            return b;
        },

    },
    mounted() {
        this.btnList = JSON.parse(localStorage.getItem("arr"));
    }
}
